package com.vhans.aapi.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.vhans.core.annotation.OptLogger;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.dto.DeleteDTO;
import com.vhans.db.model.dto.RecommendDTO;
import com.vhans.db.model.dto.TopDTO;
import com.vhans.db.model.dto.record.RecordDTO;
import com.vhans.db.model.vo.record.RecordInfoVO;
import com.vhans.db.model.vo.record.RecordVO;
import com.vhans.db.service.IBRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.vhans.core.constant.OptTypeConstant.*;

/**
 * 记录控制器
 *
 * @author vhans
 **/
@Api(tags = "记录模块")
@RestController
public class RecordController extends BaseController {

    @Autowired
    private IBRecordService recordService;

    /**
     * 查看记录列表
     *
     * @param condition 条件
     * @return {@link TableDataInfo} 记录列表
     */
    @ApiOperation(value = "查看记录列表")
    @SaCheckPermission("agree:record:list")
    @GetMapping("/admin/record/list")
    public TableDataInfo listRecordVO(ConditionDTO condition) {
        startPage();
        List<RecordVO> list = recordService.listRecordVO(condition);
        return getDataTable(list);
    }

    /**
     * 添加记录
     *
     * @param record 记录信息
     * @return {@link Result<>}
     */
    @OptLogger(value = ADD)
    @ApiOperation(value = "添加记录")
    @SaCheckPermission("agree:record:add")
    @PostMapping("/admin/record/add")
    public Result<?> addRecord(@Validated @RequestBody RecordDTO record) {
        recordService.addRecord(record);
        return Result.success();
    }

    /**
     * 删除记录
     *
     * @param recordIdList 记录id集合
     * @return {@link Result<>}
     */
    @OptLogger(value = DELETE)
    @ApiOperation(value = "删除记录")
    @SaCheckPermission("agree:record:delete")
    @DeleteMapping("/admin/record/delete")
    public Result<?> deleteRecord(@RequestBody List<Integer> recordIdList) {
        recordService.deleteRecord(recordIdList);
        return Result.success();
    }

    /**
     * 回收或恢复记录
     *
     * @param delete 逻辑删除
     * @return {@link Result<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "回收或恢复记录")
    @SaCheckPermission("agree:record:recycle")
    @PutMapping("/admin/record/recycle")
    public Result<?> updateRecordDelete(@Validated @RequestBody DeleteDTO delete) {
        recordService.updateRecordDelete(delete);
        return Result.success();
    }

    /**
     * 修改记录
     *
     * @param record 记录信息
     * @return {@link Result<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改记录")
    @SaCheckPermission("agree:record:update")
    @PutMapping("/admin/record/update")
    public Result<?> updateRecord(@Validated @RequestBody RecordDTO record) {
        recordService.updateRecord(record);
        return Result.success();
    }

    /**
     * 编辑记录
     *
     * @param recordId 记录id
     * @return {@link Result<RecordInfoVO>} 后台记录
     */
    @ApiOperation(value = "编辑记录")
    @SaCheckPermission("agree:record:edit")
    @GetMapping("/admin/record/edit/{recordId}")
    public Result<RecordInfoVO> editRecord(@PathVariable("recordId") Integer recordId) {
        return Result.success(recordService.editRecord(recordId));
    }

    /**
     * 上传记录图片
     *
     * @param file 文件
     * @return {@link Result<String>} 记录图片地址
     */
    @OptLogger(value = UPLOAD)
    @ApiOperation(value = "上传记录图片")
    @ApiImplicitParam(name = "file", value = "记录图片", required = true, dataType = "MultipartFile")
    @SaCheckPermission("agree:record:upload")
    @PostMapping("/admin/recordImage/upload")
    public Result<String> saveRecordImages(@RequestParam("file") MultipartFile file) {
        return Result.success(recordService.saveRecordImages(file));
    }

    /**
     * 上传记录视频
     *
     * @param file 文件
     * @return {@link Result<String>} 记录视频地址
     */
    @OptLogger(value = UPLOAD)
    @ApiOperation(value = "上传记录视频")
    @ApiImplicitParam(name = "file", value = "记录视频", required = true, dataType = "MultipartFile")
    @SaCheckPermission("agree:record:upload")
    @PostMapping("/admin/recordVideo/upload")
    public Result<String> saveRecordVideo(@RequestParam("file") MultipartFile file) {
        return Result.success(recordService.saveRecordVideo(file));
    }

    /**
     * 置顶记录
     *
     * @param top 置顶信息
     * @return {@link Result<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "置顶记录")
    @SaCheckPermission("agree:record:top")
    @PutMapping("/admin/record/top")
    public Result<?> updateRecordTop(@Validated @RequestBody TopDTO top) {
        recordService.updateRecordTop(top);
        return Result.success();
    }

    /**
     * 推荐记录
     *
     * @param recommend 推荐信息
     * @return {@link Result<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "推荐记录")
    @SaCheckPermission("agree:record:recommend")
    @PutMapping("/admin/record/recommend")
    public Result<?> updateRecordRecommend(@Validated @RequestBody RecommendDTO recommend) {
        recordService.updateRecordRecommend(recommend);
        return Result.success();
    }
}